﻿using System;
using System.Web;

namespace Core
{
    public class CookieManager
    {
        /// <summary>
        /// Gets the cookie.
        /// </summary>
        /// <param name="cookiename">The cookiename.</param>
        /// <param name="valuename">The valuename.</param>
        /// <returns></returns>
        public static string GetCookie(string cookiename, string valuename)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
            if (cookie == null) return "";
            return cookie.Values[valuename];
        }

        /// <summary>
        /// Sets the cookie.
        /// </summary>
        /// <param name="cookiename">The cookiename.</param>
        /// <param name="valuename">The valuename.</param>
        /// <param name="value">The value.</param>
        public static void SetCookie(string cookiename, string valuename, string value)
        {
            HttpCookie ckOnline = new HttpCookie(cookiename);
            ckOnline.Values[valuename] = value;
            //ckOnline.Secure = true;
            HttpContext.Current.Response.Cookies.Add(ckOnline);

        }

        /// <summary>
        /// Sets the cookie.
        /// </summary>
        /// <param name="cookiename">The cookiename.</param>
        /// <param name="valuename">The valuename.</param>
        /// <param name="value">The value.</param>
        /// <param name="expireday">The expireday.</param>
        public static void SetCookie(string cookiename, string valuename, string value, int expireday)
        {
            HttpCookie ckOnline = new HttpCookie(cookiename);
            ckOnline.Values[valuename] = value;
            //ckOnline.Secure = true;
            ckOnline.Expires = DateTime.Now.AddDays(expireday);
            HttpContext.Current.Response.Cookies.Add(ckOnline);

        }

        /// <summary>
        /// Deletes the cookie.
        /// </summary>
        /// <param name="cookiename">The cookiename.</param>
        public static void DeleteCookie(string cookiename)
        {
            HttpCookie getCK = HttpContext.Current.Response.Cookies[cookiename];
            if (getCK != null)
            {
                HttpCookie removeCookie = new HttpCookie(cookiename);
                removeCookie.Expires = DateTime.Now.AddDays(-1D);
                HttpContext.Current.Response.Cookies.Add(removeCookie);
            }
        }
    }
}
